<?php
require_once "includes/auth.php";
require_once "includes/db.php";

$userId = $_SESSION["user_id"];
$userName = $_SESSION["user_name"] ?? "Öğrenci";

// Tüm dersleri çek
$stmt = $conn->prepare("SELECT * FROM courses");
$stmt->execute();
$allCourses = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Kullanıcının kayıtlı olduğu dersleri çek
$stmt = $conn->prepare("
    SELECT c.id, c.course_name, c.description 
    FROM enrollments e
    JOIN courses c ON e.course_id = c.id
    WHERE e.user_id = ?
");
$stmt->execute([$userId]);
$myCourses = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <title>Öğrenci Paneli</title>
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
    <div class="dashboard-container">
        <h2>Hoş geldin, <?= htmlspecialchars($userName) ?>!</h2>
        <a href="logout.php" class="logout-btn">Çıkış Yap</a>
		<a href="Dersler.php" class="button purple">Aldığım Dersler</a>


        <h3>Kayıtlı Olduğun Dersler:</h3>
        <div class="course-list">
            <?php if ($myCourses): ?>
                <?php foreach ($myCourses as $course): ?>
                    <div class="course-card">
                        <h4><?= htmlspecialchars($course["course_name"]) ?></h4>
                        <p><?= htmlspecialchars($course["description"]) ?></p>
                        <form method="post" action="unenroll.php">
                            <input type="hidden" name="course_id" value="<?= $course["id"] ?>">
                            <button type="submit" class="unenroll-btn">Kaydı İptal Et</button>
                        </form>
                    </div>
                <?php endforeach; ?>
            <?php else: ?>
                <p>Henüz kayıtlı dersin yok.</p>
            <?php endif; ?>
        </div>

        <h3>Tüm Dersler:</h3>
        <div class="course-list">
            <?php foreach ($allCourses as $course): ?>
                <div class="course-card">
                    <h4><?= htmlspecialchars($course["course_name"]) ?></h4>
                    <p><?= htmlspecialchars($course["description"]) ?></p>
                    <form method="post" action="enroll.php">
                        <input type="hidden" name="course_id" value="<?= $course["id"] ?>">
                        <button type="submit">Derse Katıl</button>
                    </form>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
</body>
</html>
